package president;

import home.HomePage;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;


import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.Model;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.BKSubsidiary;
import controllers.Logs;

public class ViewSubsidiary extends WebPage{
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	
	private Model dateModel;
	private Model particularsModel;
	private Model assessmentModel;
	private Model finesModel;
	private Model interestModel;
	private Model donationsModel;
	private Model dateCreatedModel;
	private Model uploadedByModel;
	
	public ViewSubsidiary(int id) throws IOException, SQLException
	{
		add(new BookmarkablePageLink("home", PresidentHome.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		
		final int formid = id;
		BKSubsidiary bk = (BKSubsidiary)sqlMap.queryForObject("BKSubsidiary.getById", id);
	
		dateModel = new Model(bk.getDate());
		particularsModel = new Model(bk.getParticulars());
		assessmentModel = new Model(bk.getAssessment());
		finesModel = new Model(bk.getAssessment());
		interestModel = new Model(bk.getFines());
		donationsModel = new Model(bk.getDonations_solicitations());
		dateCreatedModel = new Model(bk.getDate_created());
		uploadedByModel = new Model(bk.getUploader());
		
		Form form = new Form("viewForm");
		
		final String uploadby = ((Sessions)getSession()).getDesc();
		
		Date today = new Date();
		Timestamp timestamp = new Timestamp(today.getTime());
		final String ts = timestamp.toString();
		
		form.add(new Button("approve"){
			private static final long serialVersionUID = 1L;
			
			public void onSubmit(){
			  try{
								
				BKSubsidiary bk = new BKSubsidiary();
				bk.setBks_id(formid);
				bk.setStatus("APPROVED");
				sqlMap.update("BKSubsidiary.updateStatus", bk);
				
				Logs log = new Logs();
				log.setLog_activity("Approved Subsidiary Form: " + formid);
				log.setLog_officer(uploadby);
				log.setLog_time(ts);
				sqlMap.insert("Logs.insert", log);
				
				Subsidiary page = new Subsidiary();
				setResponsePage(page);
				
			  }catch(SQLException e){
				  e.printStackTrace();
			  }catch(IOException e){
				  e.printStackTrace();
			  }
			}
		});
		
		form.add(new Button("reject"){
			private static final long serialVersionUID = 1L;
			
			public void onSubmit(){
			  try{
								
				BKSubsidiary bk = new BKSubsidiary();
				bk.setBks_id(formid);
				bk.setStatus("REJECTED");
				sqlMap.update("BKSubsidiary.updateStatus", bk);
				
				Logs log = new Logs();
				log.setLog_activity("Rejected Subsidiary Form: " + formid);
				log.setLog_officer(uploadby);
				log.setLog_time(ts);
				sqlMap.insert("Logs.insert", log);
				
				Subsidiary page = new Subsidiary();
				setResponsePage(page);
				
			  }catch(SQLException e){
				  e.printStackTrace();
			  }catch(IOException e){
				  e.printStackTrace();
			  }
			}
		});
		
		
		TextField date = new TextField("date", dateModel);
		form.add(date);
		
		TextField particulars = new TextField("particulars", particularsModel);
		form.add(particulars);
		
		TextField assessment = new TextField("assessment", assessmentModel);
		form.add(assessment);
		
		TextField fines = new TextField("fines", finesModel);
		form.add(fines);
		
		TextField interest = new TextField("interest", interestModel);
		form.add(interest);
		
		TextField donations = new TextField("donation", donationsModel);
		form.add(donations);
		
		TextField dateCreated = new TextField("dateCreated", dateCreatedModel);
		form.add(dateCreated);
		
		TextField uploader = new TextField("uploader", uploadedByModel);
		form.add(uploader);
		
		add(form);
	}

}
